import { Article } from "list-model.js";
var article = new Article();

Page({
  data: {
    page:1,
    loadingHidden: false,
    searchLoading: false,
    searchLoadingComplete: false,
  },
  onLoad: function (options) {
    article.getVoteList(1,(data)=>{
      var articleListData= [];
      for(var i =0;i<data.items.length;i++){
        articleListData[i] =  data.items[i].article
      }

      this.setData({
        articleListData: articleListData,
        pageCount:data._meta.pageCount,
        loadingHidden: true,
      })
      if (data._meta.pageCount == 1) {
   
        this.setData({
          searchLoading: false,
          searchLoadingComplete: true,
        })
      }

    })
  },


  //触底滚动加载
  onReachBottom: function () {
    var page = parseInt(this.data.page) + 1;
    if (page <= this.data.pageCount) {
      this.setData({
        searchLoading: true,
      })
      article.getVoteList(page, (res) => {
        this.scrollLoadData(res, page);
      })
    }
  },
  //拼接数据
  scrollLoadData: function (res, page) {

    var data = [];
    for (var i = 0; i < res.items.length; i++) {
      data[i] = res.items[i].article
    }
    var articleListData= this.data.articleListData;
    articleListData= articleListData.concat(data)
    this.setData({
      articleListData: articleListData,
      page: page,
      pageCount: res._meta.pageCount,
    })
    if (page == this.data.pageCount) {
      this.setData({
        searchLoading: false,
        searchLoadingComplete: true,
      })
    }
  },
  //跳转到新闻详情
  toArticleDetail: function (event) {
    var id = article.getDataSet(event, 'id')
    wx.navigateTo({
      url: '../article/detail?id=' + id
    })
  }
})